$(document).ready(function() {
	//用户增加或者减少商品
	$('.itemcheckbox').change(function(event) {
		
		//计算增加了多少钱
		//获取下一个td
		var tr = $(this).parents("tr").first();
		var price = parseFloat(tr.find("code.price").first().html());
		var num = parseInt(tr.find("label.itemnum").first().html());
		var sum = price*num;


		
		if (this.checked == false) {
			addCost(-sum);
		}else {
			addCost(sum);
		}
		console.log(cartsum);
		
	});

	$('.item-minus').click(function(event) {
		/* Act on the event */
		var label = $(this).next();
		var num = parseInt(label.html());
		//获取商品价格
		var tr = $(this).parents("tr").first();
		var price = parseFloat(tr.find("code.price").first().html());
		console.log(num);
		if(num > 0) {
			num -= 1;
			addCost(-price);
			label.html(num);
		}
	});

		$('.item-plus').click(function(event) {
		/* Act on the event */
		var label = $(this).prev();
		var num = parseInt(label.html());
		var tr = $(this).parents("tr").first();
		var price = parseFloat(tr.find("code.price").first().html());
		console.log(num);
		num += 1;
		addCost(price);
		label.html(num);
	});

		//提交订单
	$('#bt-commit-order').click(function(event) {
		/* Act on the event */
		var sum_price = parseFloat($('#cartsum').html(cartsum));
		var category = "干果食品";
		var bt = $(this);
		bt.removeClass('greenbt');
		bt.addClass('redbt')
		$.ajax({
			url: '/wx/pay/commitorder',
			type: 'GET',
			data: {itemids: "1#1"}
		})
		.done(function(data) {
			window.location.href = data;
		})
		.fail(function() {
			console.log("error");
		})
		.always(function() {
			console.log("complete");
			bt.removeClass('redbt');
			bt.addClass('greenbt');
		});
		
	});
})

function addCost(cost) {
	var cartsum = parseFloat($('#cartsum').html());
	cartsum += cost;
	$('#cartsum').html(cartsum);
}